package com.learn.demo.service;

import com.learn.demo.constants.DeadLetterMsgConstants;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 死信消息监听
 *
 * @author banjiawei
 * @date 2021/12/12
 */
@Component
@Slf4j
public class DeadLetterMsgListener {

    @RabbitListener(queues = DeadLetterMsgConstants.DEAD_LETTER_QUEUE, ackMode = "MANUAL")
    public void onLazyMessage(Message msg, Channel channel) throws IOException {
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        String message = new String(msg.getBody());
        if(StringUtils.isNotBlank(message)){
            log.info("dead letter message {}", new String(msg.getBody()));
            /*单个消息手动ack确认*/
            channel.basicAck(deliveryTag, false);
        }
        log.info("deliveryTag {}", deliveryTag);
    }
}
